package sctu.edu.java.year2016.team06.liuchuanjiao.day20160612.chapter15_2;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

/**
 * Created by LCJ on 2016/6/17.
 */
public class JframThread extends JFrame {
    private final JTextArea jtextArea;
    private JPanel con;
    Timer timer;
    int row = 1;

    public JframThread() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 250);
        setLocationRelativeTo(null);
        con = new JPanel();
        con.setBorder(new EmptyBorder(10, 10, 10, 10));
        con.setLayout(new BorderLayout(10, 10));
        setContentPane(con);
        JPanel panel = new JPanel();
        con.add(panel, BorderLayout.SOUTH);


        JButton jbStart = new JButton("START");
        jbStart.setForeground(Color.GREEN);
        panel.add(jbStart);
        jbStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.start();
            }
        });

        timer = new Timer(500, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtextArea.append("this is "+row+"th row\r\n");
                row++;

            }
        });

        JButton jbStop = new JButton("PAUSE");
        jbStop.setForeground(Color.RED);
        panel.add(jbStop);
        jbStop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
            }
        });

        JScrollPane scrollPane = new JScrollPane();
        con.add(scrollPane, BorderLayout.CENTER);
        jtextArea = new JTextArea();
        scrollPane.setViewportView(jtextArea);//创建一个视口
    }

    public static void main(String[] args) {
        JframThread frame = new JframThread();
        frame.setVisible(true);
    }
}
